#-*- coding: utf8 -*-

from kbasic import *
B = Basis()
ref = InformationUnit(B, 'некоторый', B.int1)

def _ref(concept):
    return KIntentionalQuantifier(ref, concept)

phys_object = TSort(B, 'физ.об.')
int_system = TSort(B, 'инт.сист.')
active_object = TElementary([phys_object, int_system])
color = TSort(B, 'цвет')
status = TSort(B, 'статус')

cat = TSort(B, 'кошка')
dog = TSort(B, 'собака')
human = TSort(B, 'человек')

B.GenSet.update(set([(int_system, cat), (int_system, dog), (int_system, human)]))

pet = InformationUnit(B, 'домашнее животное', status)
black = InformationUnit(B, 'черный', color)
grey = InformationUnit(B, 'серый', color)

be_friends = Relation(B, 'друзья', [int_system, int_system])
be = Relation(B, 'является', [B.object, status])
implication = Relation(B, 'если-то', [B.proposal, B.proposal])

# собака - друг человека
friends_dog_human = KRelation(be_friends, [_ref(dog), _ref(human)])

# кошки и собаки не дружат
not_friends_cat_dog = KNegative(KRelation(be_friends, [_ref(cat), _ref(dog)]))

# некоторая чёрная кошка и некоторая серая собака - домашние животные
black_cat = KConcept(cat, {color: black})
grey_dog = KConcept(dog, {color: grey})
some_black_cat = KIntentionalQuantifier(ref, black_cat)
some_grey_dog = KIntentionalQuantifier(ref, grey_dog)
cat_n_dog = KAnd([some_black_cat, some_grey_dog])
human_animals = KRelation(be,[cat_n_dog, pet])

# домашняя серая кошка дружит с бездомным котом
grey_cat = KConcept(cat, {color:grey, be:pet})
orphaned_cat = KConcept(cat, {be: KNegative(pet)})
cat_friendship = KRelation(be_friends, [_ref(grey_cat), _ref(orphaned_cat)])

# дружба - симметрична...
o1 = Variable(B, 'o1', int_system)
o2 = Variable(B, 'o2', int_system)
simmetric_friendship = p9(ck_forall, o1, int_system, p9(ck_forall, o2, int_system, p4(implication, [p4(be_friends, [o1, o2]), p4(be_friends, [o2,o1])])))

# ... но не транзитивна
o3 = Variable(B, 'o3', int_system)
non_transitive_friendship = p6(p9(ck_forall, o1, int_system, p9(ck_forall, o2, int_system, p9(ck_forall, o3, int_system, p4(implication, [p7and([p4(be_friends, [o1, o2]), p4(be_friends, [o2, o3])]), p4(be_friends, [o1,o3])])))))

knowledge_database = p10([
  p5(friends_dog_human, Variable(B, 'p1', B.proposal)),
  p5(not_friends_cat_dog, Variable(B, 'p2', B.proposal)),
  p5(human_animals, Variable(B, 'p3', B.proposal)),
  p5(cat_friendship, Variable(B, 'p4', B.proposal)),
  p5(simmetric_friendship, Variable(B, 'p5', B.proposal)),
  p5(non_transitive_friendship, Variable(B, 'p6', B.proposal))
])

